package hector.tang.util;

import org.springframework.core.convert.converter.Converter;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 03 自定义类型转换器  2020-01-01格式日期转换成Date日期对象
 *Converter<S, T>
 */
public class StringToDateConvert implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        if(source==null){
            throw new RuntimeException("传入日期为空");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            LocalDate dateTime = LocalDate.parse(source, formatter);
            ZonedDateTime zonedDateTime = dateTime.atStartOfDay(ZoneId.systemDefault());
            return Date.from(zonedDateTime.toInstant());
        } catch (Exception e) {
            throw new RuntimeException("传入日期格式错误");
        }
    }

    public static void main(String[] args) {
        DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS");
//        LocalDate date=LocalDate.parse("2020-01-01 00:00:00",df);
        LocalDate date=LocalDate.now();
        ZonedDateTime zDate=date.atStartOfDay(ZoneId.systemDefault());
        System.out.println();
    }
}
